﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Diagnostics;

namespace CreateCJK
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, UnicodeWord> unicodeWords = readUnicode();
            Dictionary<string, string> keys = readGB18030();
            string code = "unicode";
            string uncode = "un_unicode";
            StreamWriter wubi86 = new StreamWriter(code + "_wubi86.txt", false, new UTF8Encoding(false));
            StreamWriter wubi98 = new StreamWriter(code + "_wubi98.txt", false, new UTF8Encoding(false));
            StreamWriter wubi06 = new StreamWriter(code + "_wubi06.txt", false, new UTF8Encoding(false));

            StreamWriter un_wubi86 = new StreamWriter(uncode + "_wubi86.txt", false, new UTF8Encoding(false));
            StreamWriter un_wubi98 = new StreamWriter(uncode + "_wubi98.txt", false, new UTF8Encoding(false));
            StreamWriter un_wubi06 = new StreamWriter(uncode + "_wubi06.txt", false, new UTF8Encoding(false));

            foreach (KeyValuePair<string,UnicodeWord> kvp in unicodeWords)
            {
                //if (!keys.ContainsKey(kvp.Value.HanZi))
                //    continue;

                

                if(!string.IsNullOrEmpty( kvp.Value.WuBi86))
                    wubi86.Write(kvp.Value.HanZi + "\t" + kvp.Value.WuBi86 + "\r\n");
                else
                    un_wubi86.Write(kvp.Value.Unicode + "\t" + kvp.Value.HanZi +"\r\n");
                


                if (!string.IsNullOrEmpty(kvp.Value.WuBi98))
                    wubi98.Write(kvp.Value.HanZi + "\t" + kvp.Value.WuBi98 + "\r\n");
                else
                    un_wubi98.Write(kvp.Value.Unicode + "\t" + kvp.Value.HanZi + "\r\n");

                if (!string.IsNullOrEmpty(kvp.Value.WuBi06))
                    wubi06.Write(kvp.Value.HanZi + "\t" + kvp.Value.WuBi06 + "\r\n");
                else
                    un_wubi06.Write(kvp.Value.Unicode + "\t" + kvp.Value.HanZi + "\r\n");
            }
            wubi86.Flush();
            wubi86.Close();

            wubi06.Flush();
            wubi06.Close();

            wubi98.Flush();
            wubi98.Close();

            un_wubi86.Flush();
            un_wubi86.Close();

            un_wubi06.Flush();
            un_wubi06.Close();

            un_wubi98.Flush();
            un_wubi98.Close();


            Console.WriteLine("完成");

            Console.ReadLine();
        }
        static Dictionary<string, UnicodeWord> readUnicode()
        {
            Dictionary<string, UnicodeWord> unicodeWords = new Dictionary<string, UnicodeWord>();


            Stopwatch st1 = new Stopwatch();//计算运行时间
            String[] files = Directory.GetFiles("UnicodeCJK", "*.*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                Console.WriteLine("开始读取：" + file);
                st1.Start();
                using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Length > 0)
                        {
                            string[] info = line.Split(new string[] { "," }, StringSplitOptions.None);

                            UnicodeWord word = new UnicodeWord(info[0], info[1], info[2], info[3], info[4], info[5], info[6]);

                           try
                            {
                                unicodeWords.Add(info[1], word);
                            }
                            catch 
                            {
                                Console.WriteLine("遇到错误：" + line);
                            }
                        }
                    }
                }
                st1.Stop();
                Console.WriteLine("读取成功：" + file);
                Console.WriteLine("耗时：" + st1.ElapsedMilliseconds);
                st1.Reset();
            }

            return unicodeWords;
        }
    
        static Dictionary<string ,string> readGB18030()
        {
            Dictionary<string, string> keys = new Dictionary<string, string>();
            Stopwatch st1 = new Stopwatch();//计算运行时间
            String[] files = Directory.GetFiles("单字", "*.*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                st1.Start();
                using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Length > 0)
                        {
                            string[] ArrWordCode = line.Split(new string[] { "\t" }, StringSplitOptions.None);
                            string keyWord = ArrWordCode[0].ToString();
                            string codeWord = ArrWordCode[1].ToString();

                            if(!keys.ContainsKey(keyWord))
                                keys.Add(keyWord, codeWord);

                        }
                    }
                }
                st1.Stop();
                Console.WriteLine("读取成功：" + file);
                Console.WriteLine("耗时：" + st1.ElapsedMilliseconds);
                st1.Reset();
            }

         return keys;
        }
    }
}
